package com.babarus.netmonitor;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.content.Context;
import android.util.Log;

import com.babarus.netmonitor.ItemMaster;
import com.babarus.netmonitor.ItemXMLHandler;

public class ParseResponce {
	private String xmlContent;
	private boolean errorResponce;
	
	public ParseResponce(String xml, Context context){
		this.errorResponce = false;
		this.xmlContent = xml;
		String errorMsg = context.getResources().getString(R.string.error_no_response);
		if(xml.equals(errorMsg)){
			this.errorResponce = true;
			this.xmlContent = "";
		}
	}
	

	public PicWebModel parseXML() {
			PicWebModel currentDeviceState = new PicWebModel();
			if (this.errorResponce == true) {
				return currentDeviceState;
			}
	        String parsedData = "";
	        try {
	            /** Handling XML */
	            SAXParserFactory spf = SAXParserFactory.newInstance();
	            SAXParser sp = spf.newSAXParser();
	            XMLReader xr = sp.getXMLReader();
	 
	            ItemXMLHandler myXMLHandler = new ItemXMLHandler();
	            xr.setContentHandler(myXMLHandler);
	            InputSource inStream = new InputSource();

	            inStream.setCharacterStream(new StringReader(xmlContent));
	 
	            xr.parse(inStream);
	 
	            ArrayList<ItemMaster> itemsList = myXMLHandler.getItemsList();
	            currentDeviceState.putData(itemsList);
	            
	            for(int i=0;i<itemsList.size();i++){
	                ItemMaster item = itemsList.get(i);
	                
	                if(item.getName().equalsIgnoreCase("temp0")){ 
	                	//temp0.setText(item.getValue());
	                	//Log.w("val","temp "+item.getValue());
	                }
	                if(item.getName().equalsIgnoreCase("pot0")){
	                	//pot0.setText(item.getValue());
	                	//Log.w("val","pot0 "+item.getValue());
	                }
	                if(item.getName().equalsIgnoreCase("led1")){ 
	                	//Log.w("val","led1 "+item.getValue());
	                	
	                	if(item.getValue().equalsIgnoreCase("1")){
	                		//tBtn.setChecked(false);
	                	}else{
	                		//tBtn.setChecked(true);
	                	}
	                }
	               //dbAdaptor.insertParam( item.getName(), item.getValue());
	                
	            }
	            Log.w("AndroidParseXMLActivity", "Done");
	        }
	        catch (Exception e) {
	            Log.w("AndroidParseXMLActivity",e );
	        }
	        
	        return currentDeviceState;
	    }   
	    



	
	

}
